//
//  NetworkController.m
//  YYZB
//
//  Created by 顾悦 on 16/3/21.
//
//

#import "NetworkController.h"

#import "NSDictionaryNullKiller.h"

@implementation NetworkController

- (void)getFromURL:(NSString *)URLString
 completionHandler:(Completion)completionHandler {
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:URLString] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error != nil) {
            completionHandler(nil, error);
        } else {
//            NSString *r = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"%@ - %@", URLString, r);
            NSError *error;
            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            if (JSON) {
                NSData *jsonData = [NSJSONSerialization dataWithJSONObject:JSON
                                                                   options:NSJSONWritingPrettyPrinted
                                                                     error:nil];
                NSString *jsonStr = [[NSString alloc] initWithData:jsonData
                                                          encoding:NSUTF8StringEncoding];
                NSLog(@"返回数据----%@ - %@", URLString, jsonStr);
            }
            
            JSON = [NSDictionaryNullKiller dataByReplacingNullWithEmptyString:JSON];
            completionHandler(JSON, error);
        }
    }];
    [task resume];
}

@end
